package dao;

import java.sql.*;
import java.util.*;

import model.*;

public class RemindDao {

    private static RemindDao remindDao = new RemindDao();
    private RemindDao(){}
    public static RemindDao getInstance() {
        return remindDao;
    }
    
   //添加预约提醒
    public boolean insertRemind(int uId, int rid) {
        String sql = "insert into Remind(uid, rid,isread)values(?,?,?)";
        Connection conn = BaseConn.getConn();
        PreparedStatement pstm=null;
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setObject(1, uId);
            pstm.setObject(2, rid);
            pstm.setObject(3, 0);
            return pstm.executeUpdate()>0;
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                pstm.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    
    // 书籍可借提醒
     
    public List<Room> getRemindByUid(int uid) {
        List<Room> rooms = new ArrayList<Room>();
        String sql = "select type ,id from Remind, Room " +
        		"where Remind.rid = Room.rid and isRead = 0 and Room.count > Room.hasrented and uid="+uid;
        Connection conn = BaseConn.getConn();
        ResultSet rs = null;
        PreparedStatement pstm = null;
        try {
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();// 执行
            while (rs.next()) {
                Room room = new Room();
                room.setId(rs.getInt("id"));
                room.setType(rs.getString("type"));                
                rooms.add(room);
            }
            return rooms;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
                pstm.close();
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    
    //批量更新为已读提醒
    public void updateRemindBatch(String str) {
        String sql = "update Remind set isRead = 1 where id in ("+str+")";
        Connection conn = BaseConn.getConn();
        PreparedStatement pstm = null;
        try {
            pstm = conn.prepareStatement(sql);
            pstm.executeUpdate();// 执行
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
               pstm.close();
               conn.close();
                
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
